package com.googlecode.hyakutaded;

import com.googlecode.hyakutaded.json.JSONException;
import com.googlecode.hyakutaded.json.JSONObject;

public class Packet {
	private long id;
	private JSONObject content = new JSONObject();
	private int edits = 0;

	public Packet(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void set(String id, JSONObject data) throws JSONException {
		content.put(id, data);
		this.edits++;
	}

	public void add(String id, JSONObject data) throws JSONException {
		content.accumulate(id, data);
		this.edits++;
	}

	public void update(String id, String subid, JSONObject data)
			throws JSONException {
		JSONObject o = content.getJSONObject(id);
		if (o == null) {
			o = new JSONObject();
		}
		o.put(subid, data);
		content.put(id, o);
		this.edits++;
	}

	public JSONObject getContent() throws JSONException {
		return new JSONObject().put("id", id).put("data", content);
	}

	public int getEdits() {
		return edits;
	}
}
